# -*- coding: utf-8 -*-
# @Time : 2024/6/19 1:14 PM 
# @Author : Wayne
# @Email : 1
# @File : fenzu.py
# @Project : young_python_demo
# @DESCRIPTION :

JKY_data = {"recId":"73003","vendCustomerCode":"KY","warehouseCode":"CP001","goodsdocNo":"CRK202406181318","billNo":"RK202404170019","inOutDate":"2024-06-18 16:16:01","inouttype":101,"hasNext":"false","userName":"陈思琪","comment":"HZP004067","companyId":"1889123614390191104","companyCode":"101","sourceBillNo":"CG202404170877","outBillNo":"HZP004067","goodsDocDetailList":[{"recId":"142415","goodsId":"1899402632289354752","goodsNo":"SAWS0661A,6W,3000K","unitName":"个","quantity":330,"estCost":3832.29,"estPrice":11.613,"batchNo":"06/18","sourceDetailId":"1930581389734413187","purchDetlId":"1930581389734413187"},{"recId":"142416","goodsId":"1919723613185016704","goodsNo":"SAWS1063A,10W,3000K","unitName":"个","quantity":858,"estCost":19855.84,"estPrice":23.142,"batchNo":"06/18","sourceDetailId":"1930581389742801792","purchDetlId":"1930581389742801792"}]}
goodsDocDetailList = JKY_data.get("goodsDocDetailList")
    # .get('goodsDocDetailList')
#
print(goodsDocDetailList)



def sum_by_group(data_list, group_fields, sum_fields, watch_fields):
    grouped_data = {}
    for info in data_list:
        key = tuple(info.get(field, '') for field in group_fields)
        if key not in grouped_data:
            grouped_data[key] = {
                field: float(info.get(field, 0)) if field in sum_fields else info.get(field, '')
                for field in set(group_fields + sum_fields + watch_fields)
            }
        else:
            for field in sum_fields:
                grouped_data[key][field] += float(info.get(field, 0))

    result_sum = []
    for key in grouped_data:
        temp_dict = {}
        for field in watch_fields:
            temp_dict[field] = grouped_data[key].get(field, '')
        # 确保汇总字段也在结果中
        temp_dict.update({field: str(grouped_data[key][field]) for field in sum_fields})
        result_sum.append(temp_dict)
    return result_sum


goodsDocDetailList = [
    {
        "goodsNo": "SAWS0661A,6W,3000",
        "quantity": 330
    },
    {
        "goodsNo": "SAWS1063A,10W,3000K",
        "quantity": 858
    },

]

# 分组字段
group_fields = ["goodsNo"]
# 显示字段
watch_fields = ["goodsNo", "quantity"]
# 汇总字段
sum_fields = ["quantity"]
jack_sum_goods_list = sum_by_group(goodsDocDetailList,group_fields,sum_fields,watch_fields)
print(jack_sum_goods_list)